﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml.Serialization;
namespace HyperTerminalController
{

    public struct dataStorage
    {
        public string command;
        public string[] args;
    }



    public partial class Form1 : Form
    {
        public Form1()
        {
            dataStorage test = new dataStorage();
            test.command = "p";
            string [] temp =  {"asdas","asda","asdasd"};
            test.args = temp;
            XmlSerializer serialer = new XmlSerializer(typeof(dataStorage));
            using (StreamWriter outHandle = new StreamWriter("test.xml")){
                serialer.Serialize(outHandle, test);
            }

            InitializeComponent();
        }

        public void send(string keys)
        {
            SendKeys.SendWait(keys);
            System.Threading.Thread.Sleep(200);
        }
        private void button1_Click(object sender, EventArgs e)
        {
      
           
            // simple script 
            // w = wait
            // p = picture
            // anything else is command
            TakeScreenShots test = new TakeScreenShots("hypertrm");
            //test.SetWindowForeGround();
            //Bitmap img = test.TakePicture("initial.jpg");
            //MessageBox.Show("Took picture");
            //test.SetWindowForeGround();
            //send("{ESC}");
            //send("{ESC}");
            //send("{ESC}");
            //send("{ESC}");
            //System.Threading.Thread.Sleep(2000);
            //pictureBox1.Image = test.TakePicture("mainScreen.jpg");
            //MessageBox.Show("Took picture");
            //test.SetWindowForeGround();
            //send("{ESC}");
            //send("?op{ENTER}");
            //System.Threading.Thread.Sleep(2000);
            //textBox1.Text += System.DateTime.Now.ToString() + Environment.NewLine;
            //System.Threading.Thread.Sleep(10000);
            string name = @"Initial.jpg";
            test.TakePicture(name);
            textBox1.Text += System.DateTime.Now.ToString() + Environment.NewLine;
            //MessageBox.Show("Took picture");


            //TextReader inHandle = new StreamReader("command.txt");
            //MessageBox.Show(Environment.GetCommandLineArgs()[1]);


            using (TextReader inHandle = new StreamReader(Environment.GetCommandLineArgs()[1]))
            {
                //StreamReader("command.txt");
                string temp;
                int i = 0;
                while ((temp = inHandle.ReadLine()) != null)
                {
                    if(temp=="")
                        goto commentLine;

                    if (temp[0] == ';' )
                        goto commentLine;
                    
                    if (temp.Contains(";"))
                    {
                        string[] split = temp.Split(';');
                        temp = split[0];
                    }

                    if (temp[0] == 'p')
                    {
                        
                        test.TakePicture(temp.Substring(1).Trim() + ".jpg");
                    }
                    else if (temp == "w")
                    {
                        System.Threading.Thread.Sleep(1000);
                    }
                    else if (temp[0] == 'd')
                    {
                        MessageBox.Show(temp.Substring(1).Trim());
                        //MessageBox.Show(inHandle.ReadLine());
                    }
                    else
                    {
                        test.SetWindowForeGround();
                        System.Threading.Thread.Sleep(100);
                        send(temp);
                    }

                    //pictureBox1.Image = img;
                    //if the line is just a comment or empty line just skip the entire evaluation process
                    commentLine:
                    
                    i++;
                }
            }
            //textBox1.Text = test.message;
        }
    }
}
